package com.cff.netty;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.util.Map;
import com.cff.proxy.HandlerProxy;
import com.cff.service.HandlerService;

public class NettyServerHandler extends SimpleChannelInboundHandler<String>{
	private Map<String, Object> exportServiceMap;
	
	public NettyServerHandler(Map<String, Object> exportServiceMap) {
		this.exportServiceMap = exportServiceMap;
	}

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {		
//		System.out.println(((HandlerServiceImp) exportServiceMap.get("helloWorldService")).test());
//		// 返回客户端消息 - 我已经接收到了你的消息
//		ctx.writeAndFlush("Received your message !\n");
		System.out.println(Thread.currentThread().getName()+"----位置6");
		HandlerService hs = new HandlerProxy(ctx, msg, exportServiceMap);
		hs.handle(msg);
	}

	@Override
	public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
		System.out.println(ctx.channel().remoteAddress() + " channelRegistered " );
		super.channelRegistered(ctx);
	}

	@Override
	public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
		System.out.println(ctx.channel().remoteAddress() + " channelUnregistered " );
		super.channelUnregistered(ctx);
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		System.out.println(ctx.channel().remoteAddress() + " channelActive " );
		super.channelActive(ctx);
	}

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		System.out.println(ctx.channel().remoteAddress() + " channelInactive " );
		super.channelInactive(ctx);
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		System.out.println(ctx.channel().remoteAddress() + " exceptionCaught :" + cause.getMessage() );
		super.exceptionCaught(ctx, cause);
	}

}
